<template>
  <div class="purchase-list-layout role-layout">
    <crumbList :navList="navList"></crumbList>
    <div class="purchase-content role-content">
      <div class="purchase-table role-table">
        <div class="purchase-item">
          <el-form ref="searchData" :model="searchData" class="clearfix">
            <el-form-item>
              <el-select v-model="searchData.number" placeholder="请选择图片来源">
                <el-option v-for="(item, index) in picOptions" :label="item.label" :value="item.value" :key="index"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item class="principalItem">
              <el-date-picker  v-model="dateScope"  type="daterange"  placeholder="选择上传时间范围" @input="dateScopeChange"></el-date-picker>
            </el-form-item>
            <el-form-item class="itemButton">
              <el-button type="primary" @click="search">查询</el-button>
              <el-button type="primary" @click="reset">重置</el-button>
              <a :href="exportExcel">
                <el-button type="primary" class="addItem" @click="exportExcelHandler">导出</el-button>
              </a>
            </el-form-item>
          </el-form>
        </div>
        <div class="purchase-table-list">
          <el-table :data="tableData" border>
            <el-table-column :label="item.label" v-for="(item,index) in table" :key="index" :width="item.name==='orderNum'?64:''">
              <template scope="scope">
                <span v-if="item.name === 'url'">
                  <el-button type="primary" class="deleteBtn" @click='showPic(scope.row)'>附件</el-button>
                </span>

                <span v-else-if="item.name === 'orderNum'">
                  <span :title="scope.row[item.name]">{{ scope.$index + 1 }}</span>
                </span>

                <span v-else :title="scope.row[item.name]">{{ scope.row[item.name] }}</span>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="block">
          <el-pagination layout="total, prev, pager, next, jumper"
                         @current-change="handleCurrentChange"
                         :current-page="page.pageNum"
                         :page-size="page.pageSize"
                         :total="page.total">
          </el-pagination>
        </div>
      </div>
    </div>
    <!--新增账号-->
    <el-dialog class="role-dialog" v-model="picDialog" title="图片" @close="dialogFormClose" top="70px">
      <div class="picShow">
        <img :src="picSrc" alt="">
        <!--<img src="/static/img/login/login.jpg" alt="">-->
      </div>
    </el-dialog>
  </div>
</template>
<script>
  import copyObject from '../../../utils/copyObject/copyObject'
  import ajax from '../../../utils/ajax/index'
  import setDate from '../../../utils/date/setDate'
  import crumbList from '../../common/crumbList.vue'

  export default {
    data () {
      return {
        navList: [
          {name: '设置', path: ''},
          {name: '图片管理', path: ''}
        ],
        dateScope: [], // 日期时间间隔
        searchData: {
          number: '',
          createTime: '',
          endTime: ''
        },
        picOptions: [
          {label: '合同附件', value: '1'},
          {label: '单据附件', value: '2'}
        ],
        tableData: [
          {
            edit: false,
            orderNum: '1',
            projectName: '武汉电子公交卡',
            source: '合同附件',
            createTime: '2017-08-12',
            url: '#'
          }
        ],
        table: [
          {label: '序号', name: 'orderNum'},
          {label: '项目', name: 'projectName'},
          {label: '图片来源', name: 'source'},
          {label: '上传时间', name: 'createTime', sortable: true},
          {label: '附件', name: 'url'}
        ],
        page: {
          pageNum: 1,
          pageSize: 10,
          total: 1,
          pages: 1
        },
        picDialog: false,
        picSrc: '',
        exportExcel: ''
      }
    },
    components: {crumbList},
    mounted () {
      this.result()
    },
    methods: {
      // 查询
      result (searchData) {
        let url = this.api.pic.result
        let store = this.$store
        ajax.get(url, searchData || {}, store, true).then((res) => {
          if (res.msg.code === '20000') {
            let data = res.data.list
            let page = res.data.page
            this.page = copyObject(page)
            this.tableData = data
          }
        })
      },
      exportExcelHandler () {
        let url = this.api.pic.export
        let params = this.searchData
        let str = '?'
        for (var key in params) {
          if (params[key] !== '') {
            str = str + key + '=' + params[key] + '&'
          }
        }
        this.exportExcel = url + str.slice(0, str.length - 1)
      },
      search () { // 搜索
        this.searchData.pageNum = 1
        this.result(this.searchData)
      },
      showPic (row) {
        this.picDialog = true
        this.picSrc = row.url
      },
      // 重置
      reset () {
        this.searchData = {
          number: '',
          createTime: '',
          endTime: ''
        }
        this.dateScope = []
      },
      handleCurrentChange (val) {
        this.searchData.pageNum = val
        this.result(this.searchData)
      },
      dialogFormClose (formName) { // 关闭新增弹窗
        this.picDialog = false
      },
      dateScopeChange (val) {
        this.searchData.createTime = setDate(val[0])
        this.searchData.endTime = setDate(val[1])
      }
    }
  }
</script>
